# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

import os

from absl import app, flags

import pandas
import matplotlib
matplotlib.use('Agg')

FLAGS = flags.FLAGS


def merge_df(df1, df2):
  compare_cols = ['json']
  merged_df = pandas.merge(df1, df2, on=compare_cols, how='inner')
  return merged_df


def read_csv_into_df(csv_path):
  df = pandas.read_csv(csv_path, sep=' ', header=None)
  df.columns = ['day', 'time', 'timestamp', 'zero1', 'zero2', 'json']
  df['datetime'] = pandas.to_datetime(df['timestamp'], unit='ns')
  df = df.set_index('datetime')
  return df


def main(argv):
  csv_path1 = FLAGS.csv_path1
  assert csv_path1, '--csv_dir1 must be specified.'
  csv_path2 = FLAGS.csv_path2
  assert csv_path2, '--csv_dir2 must be specified.'

  df1 = read_csv_into_df(csv_path1)
  print('df1 len= %d' % len(df1.index))
  df2 = read_csv_into_df(csv_path2)
  print('df2 len= %d' % len(df2.index))

  merged_df = merge_df(df1, df2)
  print('merged_df len= %d' % len(merged_df.index))

  return 0


if __name__ == '__main__':
  flags.DEFINE_string('csv_path1', None, 'Intput path1.')

  flags.DEFINE_string('csv_path2', None, 'Intput path2.')

  app.run(main)
